<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

function get_encode_password(string $password)
{
    return md5(base64_encode(md5(md5(base64_encode($password)))));
}

function sign_in_follow_operation(array $adminUserInfo)
{
    $adminRole = \app\controller\AdminUsersRole::getUsersRole(new \app\model\AdminUsersRole(), $adminUserInfo['id']);
    $adminRoleMenu = \app\controller\AdminRoleMenu::getSignInUserMenuPermisson(new \app\model\AdminRoleMenu(), $adminRole[0]['role_id']);
    cache('admin_user_info', $adminUserInfo);
    foreach ($adminRoleMenu as $k => $v)
    {
        $adminMenuData[] = $adminRoleMenu[$k]['menu_list'];
    }
    foreach ($adminMenuData as $menuValue)
    {
        foreach ($menuValue as $childValue)
        {
            $adminInitMenuData[] = $childValue;
        }
    }
    record_admin_login_log(request()->ip());
    set_menu_cache(array_unique($adminInitMenuData, SORT_REGULAR));
    return json(200);
}

function update_user_info_follow_operation()
{
    record_update_user_info_operating_log();
    \think\facade\Cache::clear();
    return json(200);
}

function set_menu_cache(array $menuData)
{
    cache('admin_menu', $menuData);
}

function get_menu_cache()
{
    return cache('admin_menu');
}

function get_admin_user_info() : array
{
    $adminUserInfo = cache('admin_user_info');
    return $adminUserInfo;
}

function record_add_top_menu_log(string $menuName)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = '增加了 顶级菜单 '.$menuName;
    return $systemOperatingLog->record();
}

function record_delete_menu_log(int $menuId)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = '删除了 ID 编号为 '.$menuId.' 的菜单';
    return $systemOperatingLog->record();
}

function record_search_operating_log(string $startTime, string $endTime, string $account)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = '查询账号 '.$account.' '.$startTime.' 到 '.$endTime.' 的操作日志';
    return $systemOperatingLog->record();
}

function record_edit_menu_operating_log(int $menuId)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = '修改菜单编号 ID 为 '.$menuId.' 的信息';
    return $systemOperatingLog->record();
}

function record_add_son_menu_operating_log(string $menuName)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = '添加子菜单 '.$menuName;
    return $systemOperatingLog->record();
}

function record_delete_admin_user_operating_log(int $adminUserId)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = '删除编号为 '.$adminUserId.' 的管理员';
    return $systemOperatingLog->record();
}

function record_delete_admin_role_operating_log(int $adminRoleId)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = '删除编号为 '.$adminRoleId.' 的角色';
    return $systemOperatingLog->record();
}

function record_disable_admin_role_operating_log(int $id)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = '禁用编号为 '.$id.' 的角色';
    return $systemOperatingLog->record();
}

function record_un_disable_admin_role_operating_log(int $id)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = '启用编号为 '.$id.' 的角色';
    return $systemOperatingLog->record();
}

function record_disable_admin_user_operating_log(int $id)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = '禁用编号为 '.$id.' 的管理员';
    return $systemOperatingLog->record();
}

function record_un_disable_admin_user_operating_log(int $id)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = '启用编号为 '.$id.' 的管理员';
    return $systemOperatingLog->record();
}

function record_add_admin_user_operating_log(string $account)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = '增加管理员 '.$account;
    return $systemOperatingLog->record();
}

function record_edit_admin_user_operating_log(int $id)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = '编辑编号为 '.$id.' 的管理员';
    return $systemOperatingLog->record();
}

function record_add_admin_role_operating_log(string $roleName)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = '增加角色 '.$roleName;
    return $systemOperatingLog->record();
}

function record_edit_admin_role_operating_log(int $id)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = '编辑编号为 '.$id.' 的角色';
    return $systemOperatingLog->record();
}

function record_search_admin_login_log(string $startTime, string $endTime, string $account)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = '查询账号 '.$account.' '.$startTime.' 到 '.$endTime.' 的管理员登录日志';
    return $systemOperatingLog->record();
}

function record_admin_login_log(string $loginIp)
{
    $adminLoginLog = new \app\model\AdminLoginLog();
    $adminLoginLog->loginIp = $loginIp;
    return $adminLoginLog->record();
}

function record_update_user_info_operating_log()
{
    $adminLoginLog = new \app\model\SystemOperatingLog();
    $adminLoginLog->operating = '修改了个人信息';
    return $adminLoginLog->record();
}